/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../xos/head.h"
#include "event_impl.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 
namespace xos
{
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    static event_impl::ITEM_DATA xos_item_data;

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    event_impl::event_impl()
    {
        m_hEvent = 0;
    }

    event_impl::~event_impl()
    {
        term();
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int event_impl::get_xos_item_data( ITEM_DATA*& item_ptr )
    {
        int ret = 0;
        xos_item_data.set_container( mgr::container() );
        item_ptr = &xos_item_data;
        return ret;
    }

    int event_impl::init()
    {
        int nRet = 0;

        if( !m_hEvent )
        {
            m_hEvent = CreateEvent( 0, FALSE, FALSE, 0 );
        }

        ResetEvent( m_hEvent );

        return nRet;
    }

    int event_impl::term()
    {
        int nRet = 0;

        if( m_hEvent )
        {
            CloseHandle( m_hEvent );
            m_hEvent = 0;
        }

        return nRet;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    unsigned long event_impl::wait( unsigned long ulMilliseconds )
    {
        unsigned long ulRet = EVENT_WAIT_FAILED;

        int dwRet = WaitForSingleObject( m_hEvent, ulMilliseconds );

        switch( dwRet )
        {
        case WAIT_OBJECT_0:
            {
                ulRet = EVENT_WAIT_OBJECT_0;
            }
            break;
        case WAIT_TIMEOUT:
            {
                ulRet = EVENT_WAIT_TIMEOUT;
            }
            break;
        }

        return ulRet;
    }

    int event_impl::set_event()
    {
        int nRet = 0;
        SetEvent( m_hEvent );
        return nRet;
    }
} // xos
